import mysql.connector
import configparser
from PIL import Image

def create_db_connection():
    config = configparser.ConfigParser()
    config.read('config.cfg')
    database_config = config['database']

    # 配置数据库连接
    conn = mysql.connector.connect(
        host=database_config.get('host'),
        user=database_config.get('user'),
        password=database_config.get('password'),
        database=database_config.get('database')
    )

    cursor = conn.cursor()
    return conn, cursor


def compress_and_save_image(file, file_path, quality=75):
    img = Image.open(file)

    # 保存压缩后的图像
    img.save(file_path, optimize=True, quality=quality)

    return file_path